﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachingAffairsManagementSystem.WebControl;

namespace TeachingAffairsManagementSystem.UserControl.RightContent.Admin
{
    public partial class StudentManagement : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["DSStudentCommand"] = null;
                if (Request.QueryString["s_id"] != null) // 通过 QueryString 查询
                {
                    TBSearch.Text = Request.QueryString["s_id"];
                    LBSearchS_ID_Click(null, null);
                }
            }

            else
            {
                // PostBack
                if (Session["DSStudentCommand"] != null)
                {
                    DSStudent.SelectCommand = Session["DSStudentCommand"].ToString();
                }
            }
        }


        protected void LBSearchAll_Click(object sender, EventArgs e)
        {
            DSStudent.SelectCommand = $"SELECT * FROM student ORDER BY s_id DESC";
            Session["DSStudentCommand"] = DSStudent.SelectCommand;
            GVStudent.EditIndex = -1;
            GVStudent.DataBind();
        }

        protected void LBSearchS_ID_Click(object sender, EventArgs e)
        {
            try
            {
                int s_id = Convert.ToInt32(TBSearch.Text);
                DSStudent.SelectCommand = $"SELECT * FROM student WHERE s_id = {s_id} ORDER BY s_id DESC";
                Session["DSStudentCommand"] = DSStudent.SelectCommand;
                GVStudent.EditIndex = -1;
                GVStudent.DataBind();
            }
            catch
            {
                LBSearchAll_Click(null, null);
            }
        }

        protected void LBSearchUsername_OnClick(object sender, EventArgs e)
        {
            string username = TBSearch.Text;
            DSStudent.SelectCommand = $"SELECT * FROM student WHERE username like '%{username}%' ORDER BY s_id DESC";
            Session["DSStudentCommand"] = DSStudent.SelectCommand;
            GVStudent.EditIndex = -1;
            GVStudent.DataBind();
        }

        protected void LBSearchEmail_OnClick(object sender, EventArgs e)
        {
            string email = TBSearch.Text;
            DSStudent.SelectCommand = $"SELECT * FROM student WHERE email like '%{email}%' ORDER BY s_id DESC";
            Session["DSStudentCommand"] = DSStudent.SelectCommand;
            GVStudent.EditIndex = -1;
            GVStudent.DataBind();
        }

        protected void LBSearchS_Name_OnClick(object sender, EventArgs e)
        {
            string s_name = TBSearch.Text;
            DSStudent.SelectCommand = $"SELECT * FROM student WHERE name like '%{s_name}%' ORDER BY s_id DESC";
            Session["DSStudentCommand"] = DSStudent.SelectCommand;
            GVStudent.EditIndex = -1;
            GVStudent.DataBind();
        }

        protected void GVStudent_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.NewValues["password"] == null)
            {
                Alert.Show("密码不能为空", AlertBox.Level.Danger, true);
                e.Cancel = true;
                return;
            }

            if (Utils.IsEmail(e.NewValues["email"].ToString()) == false)
            {
                Alert.Show("邮箱格式不正确", AlertBox.Level.Danger, true);
                e.Cancel = true;
                return;
            }
        }

        protected void GVStudent_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            Alert.Show("修改成功", AlertBox.Level.Success, true);
        }


        protected void GVStudent_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "LookUpLessonSelection":
                    int commandIndex = Convert.ToInt32(e.CommandArgument);
                    string s_id = GVStudent.Rows[commandIndex].Cells[0].Text;
                    Utils.OpenWindow($"Main.aspx?p=lsm&s_id={s_id}");
                    break;
            }
        }

        protected void BtnInsert_Click(object sender, EventArgs e)
        {
            string username = TBUsername.Text;
            string password = TBPassword.Text;
            string email = TBEmail.Text;
            string name = TBName.Text;
            char gender = DLGender.Items[0].Selected ? '男' : '女';
            // 判断数据合法性
            if (username == "")
            {
                Alert.Show("用户名不能为空", AlertBox.Level.Danger, true);
                return;
            }

            if (password == "")
            {
                Alert.Show("密码不能为空", AlertBox.Level.Danger, true);
                return;
            }

            if (!Utils.IsEmail(email))
            {
                Alert.Show("邮箱格式不正确", AlertBox.Level.Danger, true);
                return;
            }

            if (name == "")
            {
                Alert.Show("姓名不能为空", AlertBox.Level.Danger, true);
                return;
            }

            // 判断用户名是否可用
            if (!Operation.IsUsernameAvailable(username))
            {
                Alert.Show("用户名已被使用", AlertBox.Level.Danger, true);
                return;
            }

            DSStudent.InsertParameters["username"].DefaultValue = username;
            DSStudent.InsertParameters["password"].DefaultValue = password;
            DSStudent.InsertParameters["email"].DefaultValue = email;
            DSStudent.InsertParameters["name"].DefaultValue = name;
            DSStudent.InsertParameters["gender"].DefaultValue = gender.ToString();
            DSStudent.Insert();
            GVStudent.DataBind();
            Alert.Show("插入成功", AlertBox.Level.Success, true);
        }
    }
}